

function cal_distance(seeds, DATA, OUT, market, version)

    close all
   
    format long g
    addpath('../helpers')
    addpath('../rcnl_demand')
    addpath('../rcnl_supply')

    k     = floor((seeds-1)/100)+1;
    seeds = seeds - 100*(k-1);
    rng(seeds);
    
    load(fullfile(DATA, sprintf('sim_data_sim%d_%d_%d%s.mat', k, seeds, market, version)));

    der_mod = coefs_sim.der_mod;
    der_sim = coefs_sim.der_sim;
    J       = coefs_sim.J;

    if seeds == 1
        alpha_minccs = sum(der_sim(:).*der_mod(:))/sum(der_mod(:).*der_mod(:));
        dccs_L       = (1/J)*sqrt((1/market)*sum((der_sim(:)-alpha_minccs*der_mod(:)).^2));  % 5 seconds
        
        if strcmp(version, '')
            dcsx_L       = (1/J)*sqrt(DCSX_mktwide(df_sim,coefs_sim,dfull_sim, 'L_brandfe_montfe', 100)); % 3 hours 
            dcsx_Lnom    = (1/J)*sqrt(DCSX_mktwide(df_sim,coefs_sim,dfull_sim, 'L_brandfe', 100)); % 2 hours
            dcsx_RCL     = (1/J)*sqrt(DCSX_mktwide(df_sim,coefs_sim,dfull_sim, 'RCL', 100)); % 4 hours
            dccs_RCL     = (1/J)*sqrt(DCCS_rcl(df_sim,coefs_sim,dfull_sim, 1000)); % 20 minutes 
        end
        
    end

    coefs_sim.dist_mis.dccs_L      = dccs_L;
    if strcmp(version, '')
        coefs_sim.dist_mis.dccs_RCL    = dccs_RCL;
        coefs_sim.dist_mis.dcsx_L      = dcsx_L;
        coefs_sim.dist_mis.dcsx_Lnom   = dcsx_Lnom;
        coefs_sim.dist_mis.dcsx_RCL    = dcsx_RCL;
    end

    save(fullfile(OUT, sprintf('sim_data_sim%d_%d_%d%s.mat', k, seeds, market, version)), 'df_sim', 'dfull_sim', 'coefs_sim');

end

